home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / bastips2.zip / VUPORTS.TXT < prev   
Text File  |  1986-07-02  |  10KB  |  243 lines

  1.                      Viewports in IBM BASIC
  2.           (COMPUTE! Magazine July 1985 by John Kearney)
  3.  
  4.      Lots of power is hidden within the depths of IBM BASIC.  So
  5. many statements, commands and functions are tucked away that it's
  6. difficult to assimilate them all without studying the manual from
  7. cover to cover.  One of these is the VIEW statement.  It lets you
  8. segment or define an almost unlimited number of rectangular sections
  9. on the screen.  Each viewport, as it is called, becomes a separate
  10. window with its own graphics and text, like a smaller screen pasted
  11. over the main screen.  Viewports can exist independently of each
  12. other and can be of any size within the dimensions of the screen.
  13. Some can even be invisible.  What makes viewports truly amazing is
  14. the way they handle graphics.
  15.  
  16.      To see for yourself the versatility of viewports, try running
  17. the programs below.  By examining these programs and comparing them
  18. with the results, you'll see how easy it is to use the VIEW statement.
  19.  
  20.      With VIEW, you can locate a viewport anywhere on the screen,
  21. adjust the size of the viewport, fill the viewport with color, and
  22. assign a color to the surrounding border lines.  The basic format of
  23. the VIEW statement is:
  24.  
  25. VIEW [[SCREEN] [(x1,y1)-(x2,y2) [,[attribute] [,[boundary]]]] ]
  26.  
  27.      The first step before using VIEW is to declare the screen mode.
  28. VIEW works only in the graphics modes, SCREEN 1 or SCREEN 2.  SCREEN 0
  29. is a text mode and can't be used with VIEW.  At first glance, you may
  30. think the SCREEN parameter in the VIEW statement lets you specify the
  31. screen mode, but it doesn't.  Actually, this parameter determines how
  32. other graphics statements affect the viewports and main screen
  33. background.
  34.  
  35.      After declaring the screen mode with a separate SCREEN statement,
  36. you define the size of the viewport by specifying two sets of screen
  37. coordinates.  The coordinates, naturally, correspond to the resolution
  38. available in the graphics mode you selected.  For example, SCREEN 1
  39. has 320 horizontal pixels by 200 vertical pixels, so the viewport must
  40. fit within this range:
  41.  
  42. SCREEN 1     320 x 200     4-color mode
  43. SCREEN 2     640 x 200     2-color mode
  44.  
  45. Remember that screen coordinates are numbered beginning with 0, so
  46. the actual range of coordinates in SCREEN 1 would be 0 to 319
  47. horizontally and 0 to 199 vertically.
  48.  
  49.      The first set of coordinates (x1,y1) defines the position of the
  50. viewport's upper-left corner; x1 is the horizontal coordinate and y1
  51. is the vertical coordinate.  The second set of coordinates (x2,y2)
  52. defines the lower-right corner.  If you want a very large viewport
  53. in SCREEN 1, you might specify:  VIEW (4,1)-(300,100).  If you want
  54. a very small viewport, you could specify:  VIEW (4,1)-(10,12).  If
  55. you actually enter these statements, you won't see anything happen.
  56. The viewport is there, but it's invisible.  To make it appear, you
  57. have to set the viewport apart from the main screen background by
  58. filling it with color or surrounding it with a colored border.
  59. That's the purpose of the last two parameters of the VIEW statement.
  60.  
  61.      The attribute parameter lets you fill the viewport with the color
  62. assigned to that attribute number.  The boundary parameter lets you
  63. draw a border around the viewport with the color assigned to the
  64. attribute number.  Attribute numbers can range from 0 to 15, but of
  65. course this depends on the number of colors available in the screen
  66. mode you choose.  For instance, SCREEN 1 is a four-color mode (see
  67. above), so it has four attributes numbered 0 to 3.  SCREEN 2 is a
  68. two-color mode.  The colors assigned to attributes are:
  69.  
  70. 0  black     4  red         8  gray            12  light red
  71. 1  blue      5  magenta     9  light blue      13  light magenta
  72. 2  green     6  brown      10  light green     14  yellow
  73. 3  cyan      7  white      11  light cyan      15  bright white
  74.  
  75. Keep in mind that you can assign any color to any attribute number
  76. with the PALETTE and PALETTE USING statements.  For example:
  77.  
  78. VIEW (4,1)-(300,100)         Attribute parameter is omitted, so
  79.                              viewport defaults to same color as
  80.                              screen, rendering viewport invisible
  81.  
  82. VIEW (4,1)-(300,100),4,14    Red viewport with yellow border
  83.  
  84. VIEW (4,1)-(300,100),5,7     Magenta viewport with white border
  85.  
  86. Once a viewport is defined and activated, the coordinates inside the
  87. viewport are no longer the same as the main screen coordinates.
  88.  
  89.      After you've created a viewport, you can print text or draw
  90. graphics inside it.  Since the viewport is smaller than the main
  91. screen, however, a full-size graphics figure may not fit within its
  92. boundaries.  You have to scale down the size of the figure to avoid
  93. what's called a clipping effect (the parts of the figure which don't
  94. fit are cut off, or clipped, within the viewport).  Ordinarily, this
  95. scaling requires manual calculations.  But another IBM BASIC statement
  96. -- WINDOW -- can help scale the graphics automatically.
  97.  
  98.      With WINDOW, each viewport acts as a microcosm of the main screen,
  99. so the computer automatically fits the graphics into the viewport.
  100. This scaling effect is demonstrated in the programs below.  For
  101. example, Program 1 uses identical graphics subroutines for each
  102. viewport, even though the viewports are different sizes.  For scaling
  103. purposes with viewports, you can simply insert this WINDOW statement
  104. prior to the VIEW statement:  WINDOW SCREEN (x1,y1)-(x2,y2)  where
  105. x1,y1 are the upper-left corner coordinates of the graphics mode (0,0),
  106. and x2,y2 are the lower-right corner coordinates (for instance, 319,199
  107. in SCREEN 1).
  108.  
  109.      When you set up a viewport, the coordinates within its boundaries
  110. no longer correspond to the coordinates of the main screen.  Instead,
  111. the coordinates for the upper-left corner or any viewport are (0,0),
  112. no matter where the viewport is located.
  113.  
  114.      There may be times when you don't want the automatic-scaling
  115. feature.  You can defeat it simply by leaving out the WINDOW statement.
  116. You can also experiment with another variation of the VIEW statement by
  117. including the SCREEN parameter mentioned earlier.  When SCREEN is
  118. included, all viewport coordinates coincide with the main screen
  119. coordinates.  That is, the upper-left corner coordinates correspond
  120. to the main screen coordinates at that point, rather than 0,0.  Points
  121. plotted outside the viewport boundaries won't appear on the screen.
  122.  
  123.      Like other screen statements, the CLS statement works in an
  124. interesting way with VIEW.  When your program is executing, the
  125. viewport most recently defined is your current and only active
  126. viewport.  All other viewports are inactive, as is the main screen.
  127. As a result, CLS clears only the area inside the active viewport.
  128. This may lead to some fascinating graphics effects.
  129.  
  130.      If your program no longer requires viewports, changing screen
  131. modes with the SCREEN statement removes them.  Likewise, a VIEW
  132. statement without any parameters followed by CLS defines the entire
  133. screen as a viewport and clears it, accomplishing basically the same
  134. thing.
  135.  
  136.      Most of the BASIC graphics statements work with viewports, but
  137. there are a few exceptions.  Some statements don't automatically scale
  138. themselves to fit within the viewport's boundaries.  Those what work
  139. include GET, PAINT, PRESET, PUT, PSET, POINT, LINE, VIEW, and WINDOW.
  140.  
  141.      Statements that deserve more attention are CIRCLE (the size of
  142. the circle does not automatically scale or change shape if different
  143. viewports are the same width); and DRAW, LOCATE, and PRINT, which
  144. aren't restricted to the area of an active viewport.
  145.  
  146.      Some of these effects are demonstrated by Program 2.  It starts
  147. by executing a subroutine that draws a series of concentric circles
  148. without any viewports.  Next it defines five viewports with borders
  149. and clears them with CLS.  At this point, only the final viewport is
  150. active and capable of accepting graphics commands.  Then the program
  151. repeats the same subroutine that draws the circles, showing that only
  152. parts of the circles appear in the active viewport.  Immediately
  153. afterward, the program executes a WINDOW statement and defines a very
  154. small invisible viewport at the center of the screen.  Then it repeats
  155. the circles subroutine again, showing how the circles are scaled down
  156. to the size of the single invisible viewport.
  157.  
  158. Program 1:  Automatic Viewport Scaling
  159.  
  160. 10 'Initialize
  161. 30 CLS:KEY OFF
  162. 40 SCREEN 1
  163. 50 COLOR 0,1
  164. 60 WINDOW SCREEN (0,0)-(319,199)
  165. 70 L$="BAR CHART VIEW COMPARISON"
  166. 80 GOSUB 340
  167. 90 LOCATE 22,3:PRINT "Using same input data and subroutine"
  168. 100 '
  169. 110 VIEW (20,60)-(80,150),,3
  170. 120 GOSUB 230
  171. 130 VIEW (90,20)-(120,150),,3
  172. 140 RESTORE 380
  173. 150 GOSUB 230
  174. 160 VIEW (130,80)-(299,150),,3
  175. 170 RESTORE 380
  176. 180 GOSUB 230
  177. 190 GOTO 190
  178. 200 '
  179. 210 'Graphics subroutine
  180. 220 '
  181. 230 READ N
  182. 240 DX=1/N:DDX=.75*DX*319:Y=199
  183. 250 FOR I=0 TO N-1
  184. 260 X=DX*I*319
  185. 270 READ D:D=D/1.25
  186. 280 LINE (X,Y)-(X+DDX,Y-D),2,BF
  187. 290 NEXT I
  188. 300 RETURN
  189. 310 '
  190. 320 'Center titles
  191. 330 '
  192. 340 LOCATE 1,(40-LEN(L$))/2
  193. 350 PRINT L$
  194. 360 RETURN
  195. 370 '
  196. 380 DATA 24
  197. 390 DATA 3.9,5.3,7.2,9.6
  198. 400 DATA 12.9,17.0,33.2,31.4
  199. 410 DATA 39.8,50.2,62.9,76.0
  200. 420 DATA 92,0,105.7,102.8,101.7
  201. 430 DATA 122.7,134.3,183.2,211.0
  202. 440 DATA 212.7,217.3,223.2,231.0
  203.  
  204. Program 2: Viewport Variations
  205.  
  206. 10 'Initiatlize
  207. 20 '
  208. 30 CLS:SCREEN 1:KEY OFF
  209. 40 GOSUB 330
  210. 50 '
  211. 60 'Viewport coordinates
  212. 70 '
  213. 80 A1=8:A2=8:A3=52:A4=52
  214. 90 B1=64:B2=8:B3=112:B4=112
  215. 100 C1=8:C2=66:C3=52:C4=180
  216. 110 D1=124:D2=8:D3=150:D4=180
  217. 120 E1=64:E2=124:E3=112:E4=180
  218. 130 F1=140:F2=80:F3=180:F4=120
  219. 140 '
  220. 150 'Define viewports
  221. 160 '
  222. 170 VIEW (A1,A2)-(A3,A4),,2:CLS
  223. 180 VIEW (B1,B2)-(B3,B4),,1:CLS
  224. 190 VIEW (C1,C2)-(C3,C4),,2:CLS
  225. 200 VIEW (D1,D2)-(D3,D4),,1:CLS
  226. 210 VIEW (E1,E2)-(E3,E4),,2:CLS
  227. 220 '
  228. 230 VIEW SCREEN (B1,B2)-(B3,B4)
  229. 240 GOSUB 330
  230. 250 '
  231. 260 WINDOW SCREEN (0,0)-(319-199)
  232. 270 VIEW (F1,F2)-(F3,F4)
  233. 280 GOSUB 330
  234. 290 GOTO 290
  235. 300 '
  236. 310 'Circle subroutine
  237. 320 '
  238. 330 FOR X=1 TO 100 STEP 4
  239. 340 CIRCLE (160,100),X+60,3
  240. 350 NEXT X
  241. 360 RETURN
  242.  
  243.